package com.iapplize.gcm.test;

import java.util.regex.Pattern;

import com.google.android.gcm.GCMRegistrar;
import com.iapplize.gcm.test.SendMessageDialog.SendMessageDialogListener;
import com.iapplize.http.HttpConnect;
import com.iapplize.objects.GlobalVariables;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.util.Patterns;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class EnteringActivety extends FragmentActivity implements OnClickListener ,SendMessageDialogListener{

	public ProgressDialog spinnerDialog;

	MyApplication appState;
	
	public TextView gcm_status;

	Button test_GCM_Ping;
	Button test_GCM_Users;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_entering);

		appState = ((MyApplication) getApplicationContext());
		appState.setMainActivety(this);
		
		gcm_status = (TextView) findViewById(R.id.gcm_status);

		test_GCM_Ping = (Button) findViewById(R.id.test_GCM_Ping);
		test_GCM_Ping.setOnClickListener(this);

		test_GCM_Users = (Button) findViewById(R.id.test_GCM_Users);
		test_GCM_Users.setOnClickListener(this);
		registerForContextMenu(test_GCM_Users);

		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
			GCMRegistrar.register(this,GlobalVariables.getSenderId());
			gcm_status.setText(R.string.GCM_Registering);
		} else {
			Log.v("GCM - test", "Already registered");
			gcm_status.setText(R.string.GCM_Ready);
		}
		
		

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		appState = ((MyApplication) getApplicationContext());
		appState.setMainActivety(this);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		GCMRegistrar.onDestroy(this);
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.setHeaderTitle(getString(R.string.Select_account_which_will_be_shown_in_list_to_send_GCM));

		Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
		Account[] accounts = AccountManager.get(this).getAccounts();
		for (Account account : accounts) {
			if (emailPattern.matcher(account.name).matches()) {
				menu.add(0, v.getId(), 0, account.name);
			}
		}
		
		if(menu.size()==0){
			
			Toast.makeText(this, R.string.No_user_found_on_device, Toast.LENGTH_SHORT)
			.show();
			
		}

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
			GCMRegistrar.register(this,GlobalVariables.getSenderId());
			Toast.makeText(this, R.string.Try_again_later, Toast.LENGTH_SHORT)
					.show();
		} else {

			if (appState.checkConnecion()) {

				spinnerDialog = ProgressDialog.show(this, "",getString(R.string.Connecting_to_server), true);

				HttpConnect hc1 = new HttpConnect(EnteringActivety.this,
						HttpConnect.GCM_SAVE_CLIENT);

				String[] mes = new String[3];
				mes[0] = regId;
				mes[1] = Installation.id(this).replace("-", "");
				mes[2] = item.getTitle().toString();
				hc1.execute(mes);

			} else {
				Toast.makeText(this, R.string.No_Internet_connection,
						Toast.LENGTH_SHORT).show();
			}
		}

		return true;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == test_GCM_Ping) {
			showSendMessageDialog();
		} else if (v == test_GCM_Users) {
			this.openContextMenu(test_GCM_Users);
		}
	}
	
	private void showSendMessageDialog() {
        FragmentManager fm = getSupportFragmentManager();
        SendMessageDialog sendMessageDialog = new SendMessageDialog();
        sendMessageDialog.show(fm, getString(R.string.Write_a_message));
    }
	
	public void onFinishEditDialog(String inputText) {
       
		final String regId = GCMRegistrar.getRegistrationId(this);
		if (regId.equals("")) {
			GCMRegistrar.register(this, GlobalVariables.getSenderId());
			Toast.makeText(this, R.string.Try_again_later,
					Toast.LENGTH_SHORT).show();
		} else {
			Log.v("GCM - test", "Already registered");

			if (appState.checkConnecion()) {
				
				spinnerDialog = ProgressDialog.show(this, "",
						getString(R.string.Connecting_to_server), true);

				HttpConnect hc1 = new HttpConnect(EnteringActivety.this,
						HttpConnect.GCM_SEND_MESSAGE);
				
				String [] mes = new String[2];
				mes[0] = regId;
				mes[1] = inputText.toString();
				
				hc1.execute(mes);
			}else{
				Toast.makeText(this, R.string.No_Internet_connection,
						Toast.LENGTH_SHORT).show();
			}
		}
		
		
		
    }

}
